cc.Class({
    extends: cc.Component,

    properties: {
        progressBar: cc.ProgressBar,
        descProgress: cc.Label,
    },

    onLoad () {
        window.initGameConfigData();
        console.log("网络状态--------->>>>>>>>>>>>> ", window.navigator.onLine);
        if(!window.navigator.onLine)window.onLoadPfPopTitle(this, "当前无网络，请联网后重新尝试");
        this.res = ["gameStart"]
        this.statistics = {};
        this.preLoadRes();
    },

    preLoadRes() {
        this.res.forEach(name => {
            this.statistics[name] || (this.statistics[name] = {
                cpt: 0,
                total: 0
            });
            let progressCb = (cpt, total) => {
                if (this.statistics) {
                    this.statistics[name].cpt = cpt;
                    this.statistics[name].total = total;
                    this.setProgress();
                }
            };
            let errorCb = err => {
                err && console.log(err);
            };
            if (name.indexOf("resources") > -1) {
                cc.loader.loadResDir("/", null, progressCb, errorCb);
            } else {
                cc.director.preloadScene(name, progressCb, errorCb);
            }
        })
    },

    setProgress() {
        let progress = 0;
        for (let name in this.statistics) {
            let obj = this.statistics[name];
            let pg = obj.cpt / obj.total / this.res.length;
            isNaN(pg) && (pg = 0);
            progress += pg;
        }
        if (progress >= 1) {
            cc.director.loadScene("gameStart",()=>{
                if(window.gameConfig.gameData.isMusic == "1")
                {
                    window.audioMgr.playGameAudio("music")
                }
            });
        } else {
            if (progress < this.progressBar.progress) return;
            this.progressBar.progress = progress;
            this.descProgress.string = Math.round(progress * 100) + "%";
        }
    }
});
